
package com.dailytechlab.controller;

import com.dailytechlab.model.PracticeRecord;
import com.dailytechlab.repository.PracticeRecordRepository;
import com.dailytechlab.repository.ModuleRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/practice-records")
@CrossOrigin(origins = "http://localhost:3000")
public class PracticeRecordController {
    
    @Autowired
    private PracticeRecordRepository practiceRecordRepository;
    
    @Autowired
    private ModuleRepository moduleRepository;
    
    @PostMapping
    public ResponseEntity<PracticeRecord> createPracticeRecord(@RequestBody PracticeRecordRequest request) {
        return moduleRepository.findById(request.getModuleId())
                .map(module -> {
                    PracticeRecord record = new PracticeRecord();
                    record.setModule(module);
                    record.setUserId(request.getUserId());
                    record.setCodeContent(request.getCodeContent());
                    record.setLinesOfCode(request.getLinesOfCode());
                    record.setIsCompleted(request.getIsCompleted());
                    
                    PracticeRecord savedRecord = practiceRecordRepository.save(record);
                    return ResponseEntity.ok(savedRecord);
                })
                .orElse(ResponseEntity.notFound().build());
    }
    
    @GetMapping("/user/{userId}")
    public List<PracticeRecord> getUserPracticeRecords(@PathVariable String userId) {
        return practiceRecordRepository.findByUserIdOrderByCreatedAtDesc(userId);
    }
}

class PracticeRecordRequest {
    private Long moduleId;
    private String userId;
    private String codeContent;
    private Integer linesOfCode;
    private Boolean isCompleted;
    
    // Getters and Setters
    public Long getModuleId() { return moduleId; }
    public void setModuleId(Long moduleId) { this.moduleId = moduleId; }
    
    public String getUserId() { return userId; }
    public void setUserId(String userId) { this.userId = userId; }
    
    public String getCodeContent() { return codeContent; }
    public void setCodeContent(String codeContent) { this.codeContent = codeContent; }
    
    public Integer getLinesOfCode() { return linesOfCode; }
    public void setLinesOfCode(Integer linesOfCode) { this.linesOfCode = linesOfCode; }
    
    public Boolean getIsCompleted() { return isCompleted; }
    public void setIsCompleted(Boolean isCompleted) { this.isCompleted = isCompleted; }
}
